const api = require('../../config/api.js');
const app = getApp();
let that;
Page({
  data: {
    ImgRootUrl: app.globalData.ImgRootUrl,
    id: 0,
    brand: {},
    goodsList: [],
    page: 1,
    size: 1000,
    goodsId: 0,  //组件api
    fingerEvent: null,  //组件api
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    that = this;
    that.setData({
      id: parseInt(options.id)
    });
    this.getBrand();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  getBrand: function () {
    app.util.request(api.BrandDetail, { id: that.data.id }).then(function (res) {
      let _res = res;
      if (res.errno === 0) {
        wx.setNavigationBarTitle({
          title: res.data.name + ' - 品牌详情'
        });
        that.setData({
          brand: res.data
        });

        // 请求接口，获取购物车列表
        app.cart.CartGoodsCount().then(function (res) {
          app.globalData.cartList = res.data.cartList;
          //商品数量 [对应购物车]
          that.setGoodsCount(_res.data.goodsList);
        }).catch((err) => {
          console.log(err)
        });
      }
    });
  },
  getGoodsList() {
    app.util.request(api.GoodsList, { brandId: that.data.id, page: that.data.page, size: that.data.size })
      .then(function (res) {
        if (res.errno === 0) {
          that.setData({
            goodsList: res.data.goodsList
          });
        }
      });
  },
  //设置商品数量 [对应购物车数量]
  setGoodsCount: function (goodsList) {
    let goods = app.globalData.cartList.goods;
    let _goodsList = goodsList.map(function (v) {
      if (v.spec_array == '') {
        //goods
        v.count = goods[v.id] ? parseInt(goods[v.id].count) : 0;
      } else {
        //product
        v.count = 0;
      }
      return v;
    });
    that.setData({
      goodsList: _goodsList
    });
  },
  //减
  cutNumber: function (event) {
    let i = event.currentTarget.dataset.i;  //当前分类的商品列表索引
    let type = event.currentTarget.dataset.type;  //类型：商品goods，货品product
    let goods_id = event.currentTarget.id;  //商品id
    let product_id = 0; //默认货品id
    let _goodsList = that.data.goodsList;
    if (type != 'goods') {
      //货品
      that.setData({
        goodsId: goods_id,
      });
    } else {
      //商品
      switch (_goodsList[i].count) {
        case 0:
          return false;
          break;
        case 1:
          app.cart.CartDelete(goods_id + '_' + product_id).then(function (res) {
            app.globalData.cartGoodsCount = res.data.cartTotal.goodsCount;
            that.setData({
              ["goodsList[" + i + "].count"]: 0
            });
          }).catch((err) => {

          });
          break;
        default:
          app.cart.CartAdd(goods_id, product_id, -1).then(function (res) {
            app.globalData.cartGoodsCount = res.data.cartTotal.goodsCount;
            that.setData({
              ["goodsList[" + i + "].count"]: _goodsList[i].count - 1
            });
          }).catch((err) => {

          });
          break;
      }
    }
  },
  //加
  addNumber: function (event) {
    console.log(event)
    that.setData({
      fingerEvent: event
    })
    let i = event.currentTarget.dataset.i;  //当前分类的商品列表索引
    let type = event.currentTarget.dataset.type;  //类型：商品goods，货品product
    let goods_id = event.currentTarget.id;  //商品id    
    let product_id = 0; //默认货品id
    let _goodsList = that.data.goodsList;
    if (type != 'goods') {
      //货品
    } else {
      that.setData({
        ["goodsList[" + i + "].count"]: _goodsList[i].count + 1
      });
      app.cart.CartAdd(goods_id, product_id, 1).then(function (res) {
        //成功
        app.globalData.cartGoodsCount = res.data.cartTotal.goodsCount;
      }).catch((err) => {
        //失败，回退操作
        that.setData({
          ["goodsList[" + i + "].count"]: _goodsList[i].count
        });
      });
    }
  },
})